<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use \AuctionCore\Other\Chip as Chip;
use \AuctionCore\System\AuctionException;
use \AuctionCore\Common\Util;

/**
 * 碎片管理
 * @author Xmh
 */
class ChipController extends BaseController {

    public function __construct() {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];
        
        $this->Chip = new Chip();
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index() {
        $this->display();
    }

    /**
     * 碎片列表
     */
    public function chip_list() {
        $page = I("get.p", 1);
        $desc = I("get.desc");
        $name = I("get.name");
        $size = 10;
        $params = array();
        $params['desc'] = $desc;
        $params['name'] = $name;
        //获取list
        $result = $this->Chip->get_chip_list($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 添加碎片
     */
    public function add_chip()
    {
        $this->display();
    }

    public function add_chip_post()
    {
        $name = I('post.name');
        if(empty($name)){
            $this->helpJsonResult(400, "请填写标题");
        }
        $desc = I('post.desc');
        if(empty($desc)){
            $this->helpJsonResult(400, "请填写描述");
        }
        $content = I('post.content');
        $json_type = I('post.json_type');
        if ($json_type == 1) {
            if(!$this->is_json($content)){
                $this->helpJsonResult(400, "填写内容格式不正确");
            }
        }
        if(empty($content)){
            $this->helpJsonResult(400, "请填写内容");
        }
        
        $data['name'] = $name;
        $data['desc'] = $desc;
        $data['content'] = $content;
        $res = $this->Chip->add_chip($data);
        if($res === false){
            $this->helpJsonResult(500, "添加失败");
        }
        $this->helpJsonResult(200, "添加成功");
    }
    /**
     * 编辑碎片
     */
    public function edit_chip() {
        $id = I('get.id');
        if($id < 1){
            AuctionException::make_throw();
        }
        $chip = $this->Chip->get_chip_info($id);
        $this->assign('chip',$chip);
        $this->display();
    }
    
    public function edit_chip_post(){
        $id = I('post.id');
        $name = I('post.name');
        if(empty($name)){
            $this->helpJsonResult(400, "请填写标题");
        }
        $desc = I('post.desc');
        if(empty($desc)){
            $this->helpJsonResult(400, "请填写描述");
        }
        $content = I('post.content');
        if(empty($content)){
            $this->helpJsonResult(400, "请填写内容");
        }
        $json_type = I('post.json_type');
        if ($json_type == 1) {
            if(!$this->is_json($content)){
                $this->helpJsonResult(400, "填写内容格式不正确");
            }
        }
        $data['id'] = $id;
        $data['name'] = $name;
        $data['desc'] = $desc;
        $data['content'] = $content;
        $res = $this->Chip->update_chip($data);
        if($res === false){
            $this->helpJsonResult(500, "更新失败");
        }
        $this->helpJsonResult(200, "更新成功");
        
    }
    
    // public function is_json($json){
    //     json_decode($json);
    //     return (json_last_error() == JSON_ERROR_NONE);
    // }

    public function is_json($str){
        $jObject = json_decode($str);
        return (is_object($jObject)) ? true : false;
    }
    public function add_file(){
        $pic = I('data.pic', '', '', $_FILES);
        $size = $pic['size'];
        if (!empty($pic)) {
           $flag = true;
           if ($pic['error'] != 0) {
               $flag = false;
               $rep['msg'] = '文件上传失败';
               \Think\Log::record($rep['msg'] . ',错误码：' . $pic['error'] . ',time:' . date('Y-m-d H:i:s'));
           }
           //图片上传
           if ($flag) {
               $object = 'upload/' . create_uuid() . '.' . get_extension($pic['name']);
               $pic_path = $pic['tmp_name'];
               try {
                    $aliObject = \AuctionCore\Common\Image::upload_file_by_file(OSS_BUCKET, $object, $pic_path,null,'','chip');
                    if ($aliObject) {
                    $host = get_content_cdnurl();
                    $url = $host . $aliObject;
                    }           
                    echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'','data'=>$url));
                    exit;
               } catch (AuctionException $e) {
                    echo Util::auto_jsonp_or_json($_REQUEST,array('msg'=>$e->getMessage(),'code'=>$e->getCode()));
                    exit;
               }
           }
       }
    }


}
